package com.tang.deliver.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tang.common.api.CompanyJobFeignClient;
import com.tang.common.entity.companyJob.Job;
import com.tang.common.entity.deliver.Deliver;
import com.tang.common.entity.user.Admin;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.deliver.service.DeliverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-21
 */
@RestController
@RequestMapping("/deliver")
public class DeliverController {
    @Autowired
    private DeliverService deliverService;

    @Autowired
    private CompanyJobFeignClient companyJobFeignClient;

    @RequestMapping("/deliverList")
    public Result deliverList(String keywords,Integer currentPage,Integer pageSize,String companyName,Integer state) {
        PageInfo<Deliver> list = deliverService.listDeliver(keywords, currentPage, pageSize,companyName,state);
        return Result.success(list);
    }

    @RequestMapping("/passDeliver")
    public Result passDeliver(Integer deliverId,Integer state) {

        Deliver deliver = deliverService.getById(deliverId);
        if(deliver!=null){
            deliver.setState(state);
            boolean b = deliverService.saveOrUpdate(deliver);
            if(b){
                return  Result.success("评审成功");
            }
        }
        return Result.error(RecruitExceptionEnum.HANDLE_FAILED);
    }
    @RequestMapping("/deliverResume")
    public Result deliverResume(@RequestParam("form") @RequestBody String form) {
        Deliver deliver = JSON.parseObject(form, Deliver.class);
       if(deliver!=null){
          QueryWrapper<Deliver> queryWrapper = new QueryWrapper();
          queryWrapper.eq("user_id",deliver.getUserId());
          queryWrapper.eq("delete_flag",1);
           List<Deliver> list = deliverService.list(queryWrapper);
           if(list!=null){
               for (Deliver deliver1 : list) {
                   if(deliver1.getJobId().equals(deliver.getJobId())){
                       return Result.error(RecruitExceptionEnum.HAS_DELIVER);
                   }
               }
           }
           boolean save = deliverService.save(deliver);
           if(save){
               return Result.success("投递成功");
           }
       }
            return Result.error(RecruitExceptionEnum.DELIVER_FAILED);
    }
    @RequestMapping("/myDeliverList")
    public Result myDeliverList(Integer userId,Integer state) {
        QueryWrapper query = new QueryWrapper();
        query.eq("delete_flag", 1);
        query.eq("user_id", userId);
        query.eq("state", state);
        List<Deliver> list = deliverService.list(query);
        if (list != null) {
            for (Deliver deliver : list) {
                Integer jobId = deliver.getJobId();
                Job feignJob = companyJobFeignClient.getFeignJob(jobId);
                if (feignJob != null) {
                    deliver.setJob(feignJob);
                }
            }
        }
        return Result.success(list);
    }


    @RequestMapping("/deleteDeliver")
    public Result deleteDeliver(Integer deliverId) {
        Deliver deliver = deliverService.getById(deliverId);
        if(deliver.getState()>0){
            return Result.error(RecruitExceptionEnum.DELETE_FAILED);
        }
        boolean b1 = deliverService.removeById(deliverId);
        if(b1){
            return Result.success("取消成功");
        }
        return Result.error(RecruitExceptionEnum.DELETE_FAILED);
    }

}
